<?php
namespace Admin\Controller;

class ArticlesController extends AdminController{

	public function index(){
			$model1=M('tags');
			$model2 = M('articles');
			$data = $model1->order('id desc')->select();
			$tags=$model2->order('id desc')->select();
			
			
			for($i=0;$i<count($tags);$i++){

				$base=$model1->find($tags[$i]['article_id']);
				$tags[$i]['t_name']=$base['name'];
			}

			$this->assign('data',$data);
			$this->assign('tags',$tags);
	
			$this->display();
		}

		/**
		* delete删除方法
		* @param int $id 删除的id
		*/
		public function delete(){
			$id = I('get.id/d',0);
			//删除
			if(M('articles')->delete($id) > 0 ){
				$this->success('恭喜你删除成功！',U('index'));
				//echo 1;
			} else {
				$this->error('删除失败！');
				//echo 0;
			}
		}
	//添加页面
	public function add(){
		$model = M('tags');
		$data = $model->order('id desc')->select();
		$this->assign('data',$data);

		$this->display();
	}

	public function insert(){
		//判断是否POST提交的数据
		//echo time();
		//$time=time();
		
		$time=date('y-m-d h:i:s',time()); 
		$_POST['a_time']=$time;

		if(!IS_POST){
			$this->redirect('index');
			exit;
		}
		$model = M('articles');
		
		$model->create();
	
		if($model->add($_POST) > 0){
			$this->success('恭喜你，添加成功！',U('index'));
		}else{
			$this->error('恭喜你，添加成功！');
		}

	}

public function edit(){
		$id = I('get.id/d',0);
		$data = M('articles')->find($id);
		if(!$data){
			$this->error('暂无数据');
			exit;
		}
		$model=M('tags');

		$tags=$model->order('id desc')->select();

		for($i=0;$i<count($tags);$i++){

				$base=$model->find($tags[$i]['article_id']);
				$tags[$i]['t_name']=$base['name'];
			}
		$this->assign('tags',$tags);
		$this->assign('data',$data);
		$this->display();
	}

	public function update(){
		//判断是否POST提交的数据
		if(!IS_POST){
			$this->redirect('index');
			exit;
		}
		//对数据进行创建  筛选 针对你new数据表
		$model = M('articles');

		$model->create();

		if($model->save($_POST) > 0){
			$this->success('恭喜你，修改成功！',U('index'));
		}else{
			$this->error('恭喜你，修改失败！');
		}
	}




	public function change(){
		
		$id = I('get.id/d',0);
		$model = M('articles');
		$data=$model->find($id);
		if($data['status']=='0'){
			$data['status']='1';
		}else{
			$data['status']='0';
		}

		if($model->save($data)){

			$this->success('恭喜你修改成功！',U('index'));

		}else {
			$this->error('修改失败！');
		
		}

    }


    public function choose(){
		
		$id = I('get.id/d',0);
		$model = M('articles');
		$data=$model->find($id);
		if($data['choose']=='0'){
			$data['choose']='1';
		}else{
			$data['choose']='0';
		}

		if($model->save($data)){

			$this->success('恭喜你修改成功！',U('index'));

		}else {
			$this->error('修改失败！');
		
		}

    }
	
}